#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266WebServer.h>

const char* ssid     = "tian";
const char* password = "kmmm0521";

ESP8266WiFiMulti wifiMulti;
ESP8266WebServer esp8266_server(80);

String str;

void setup() {
  Serial.begin(9600);
 
  pinMode(LED_BUILTIN, OUTPUT); //设置内置LED引脚为输出模式以便控制LED
  
  wifiMulti.addAP("tian", "kmmm0521"); // 将需要连接的一系列WiFi ID和密码输入这里
  Serial.println("Connecting ...");                            // 则尝试使用此处存储的密码进行连接。
  
  int i = 0;                                 
  while (wifiMulti.run() != WL_CONNECTED) {  // 此处的wifiMulti.run()是重点。通过wifiMulti.run()，NodeMCU将会在当前
    delay(1000);                             // 环境中搜索addAP函数所存储的WiFi。如果搜到多个存储的WiFi那么NodeMCU
    Serial.print(i++); 
    Serial.print(' ');    // 将会连接信号最强的那一个WiFi信号。
  }                                          // 一旦连接WiFI成功，wifiMulti.run()将会返回“WL_CONNECTED”。这也是
                                             // 此处while循环判断是否跳出循环的条件。
  
  // WiFi连接成功后将通过串口监视器输出连接成功信息 
  Serial.println('\n');
  Serial.print("Connected to ");
  Serial.println(WiFi.SSID());              // 通过串口监视器输出连接的WiFi名称
  Serial.print("IP address:\t");
  Serial.println(WiFi.localIP());           // 通过串口监视器输出ESP8266-NodeMCU的IP
 
  esp8266_server.begin();                           // 启动网站服务
  esp8266_server.on("/", handleRoot);
  esp8266_server.on("/0", HTTP_POST, c0);  
  esp8266_server.on("/1", HTTP_POST, c1);  
  esp8266_server.on("/2", HTTP_POST, c2);  
  esp8266_server.on("/3", HTTP_POST, c3); 
  esp8266_server.on("/4", HTTP_POST, c4); 
  esp8266_server.on("/5", HTTP_POST, c5); 
  esp8266_server.onNotFound(handleNotFound);

  str += "<form action=\"/0\" method=\"POST\"><input type=\"submit\" value=\"zidong_LED\"></form>";
  str += "<form action=\"/1\" method=\"POST\"><input type=\"submit\" value=\"ON_LED\"></form>";
  str += "<form action=\"/2\" method=\"POST\"><input type=\"submit\" value=\"OFF_LED\"></form>";
  str += "<form action=\"/5\" method=\"POST\"><input type=\"submit\" value=\"zidong_FEN\"></form>";
  str += "<form action=\"/3\" method=\"POST\"><input type=\"submit\" value=\"ON_FEN\"></form>";
  str += "<form action=\"/4\" method=\"POST\"><input type=\"submit\" value=\"OFF_FEN\"></form>";
  Serial.println("HTTP esp8266_server started");//  告知用户ESP8266网络服务功能已经启动
}

void loop(){
  esp8266_server.handleClient();
}

void handleRoot() {
  /*String str=
 "<form action=\"/0\" method=\"POST\"><input type=\"submit\" value=\"自动开关灯\"></form>
  <form action=\"/1\" method=\"POST\"><input type=\"submit\" value=\"开灯\"></form>
  <form action=\"/2\" method=\"POST\"><input type=\"submit\" value=\"关灯\"></form>
  
  <form action=\"/5\" method=\"POST\"><input type=\"submit\" value=\"自动开关电风扇\"></form>
  <form action=\"/3\" method=\"POST\"><input type=\"submit\" value=\"开电风扇\"></form>
  <form action=\"/4\" method=\"POST\"><input type=\"submit\" value=\"关电风扇\"></form>";//*/
  
  esp8266_server.send(200, "text/html", str);  
}
void c0(){
  Serial.print("0");
  esp8266_server.send(200, "text/html", str); 
}
void c1(){
  Serial.print("1");
  esp8266_server.send(200, "text/html", str); 
}
void c2(){
  Serial.print("2");
  esp8266_server.send(200, "text/html", str); 
}
void c3(){
  Serial.print("3");
  esp8266_server.send(200, "text/html", str); 
}
void c4(){
  Serial.print("4");
  esp8266_server.send(200, "text/html", str); 
}
void c5(){
  Serial.print("5");
  esp8266_server.send(200, "text/html", str); 
}

void handleNotFound(){
  esp8266_server.send(404, "text/plain", "404: Not found");
}
